package pers.li.controller.order;

import pers.li.model.TO.CarTO;
import pers.li.model.VO.CarVO;
import pers.li.service.CarService;
import pers.li.utils.JsonUtil;
import pers.li.utils.PublicUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/order/payGoods")
public class PayGoods extends HttpServlet {
    private final CarService carService = new CarService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = req.getReader();
        //2.读取数据
        String line = null;
        String body = null;
        while ((line = br.readLine()) != null) {
            body = line;
        }
        CarTO carTO = JsonUtil.parseObject(body, CarTO.class);
        CarVO carVOByTO = carService.getCarVOByTO(carTO);
        List<CarVO> list = new ArrayList<>();
        list.add(carVOByTO);
        req.getSession().setAttribute("payGoods", list);
        req.getSession().setAttribute("payGoodsJson", JsonUtil.toJsonString(list));
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        PublicUtils.responseJson(resp, res);
    }
}
